﻿using e_commerce.BLL.Data;
using e_commerce.BLL.Models;
using e_commerce.BLL.Services.Enterprise;
using e_commerce.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using e_commerce.Filter;


namespace e_commerce.Controllers
{
    public class GoodsController : Controller
    {
        // GET: Goods

        private readonly GoodsService _gservice;

        public GoodsController()
        {
            _gservice = new GoodsService();
            TempData["f-nav"] = "商品管理";
        }
        private void SetTempData(string fnav, string snav)
        {
            TempData["f-nav"] = fnav;
            TempData["s-nav"] = snav;
        }

        [PermissionFilter(permission: Permission.GoodsManager_All)]
        public ActionResult AllGoods(int page = 1)
        {
            SetTempData("商品管理", "全部商品");
            var list = GetGoodsList(0, page);
            return View(list);
        }

        [PermissionFilter(permission: Permission.GoodsManager_OnShelves)]
        public ActionResult OnShelves(int page = 1)
        {
            SetTempData("商品管理", "上架商品");
            var list = GetGoodsList((int)GoodsState.Shelves, page);
            return View(list);
        }

        private PagedList.StaticPagedList<GoodsModel> GetGoodsList(int state, int page)
        {
            int total = 0;
            int pageSize = 10;
            List<GoodsModel> list = _gservice.GetGoodsByPage(state, page, pageSize, ref total).Select(g => new GoodsModel()
            {
                GoodsId = g.GoodsId,
                Name = g.GoodsName,
                Stock = g.Stock,
                Price = g.Price,
                State = g.State,
                Mark = g.Details,
                StateDescribe = _gservice.GetGoodsState(g.State),
            }).ToList();
            var staticList = new PagedList.StaticPagedList<GoodsModel>(list, page, pageSize, total);
            return staticList;
        }


        [PermissionFilter(permission: Permission.GoodsManager_UnShelves)]
        public ActionResult UnShelves(int page = 1)
        {
            SetTempData("商品管理", "未上架商品");
            var list = GetGoodsList((int)GoodsState.UnShelves, page);
            return View(list);
        }

        [PermissionFilter(permission: Permission.GoodsManager_OffShelves)]
        public ActionResult OffShelves(int page = 1)
        {
            SetTempData("商品管理", "下架商品");
            var list = GetGoodsList((int)GoodsState.OffShelves, page);
            return View(list);
        }



        [HttpPost]
        [PermissionFilter(permission: Permission.GoodsManager_OffShelves)]
        public JsonResult OffShelvesGoods(int goodsId)
        {
            return OperateGoods(2, goodsId);
        }
        [HttpPost]
        [PermissionFilter(permission: Permission.GoodsManager_OnShelves)]
        public JsonResult OnShelvesGoods(int goodsId)
        {
            return OperateGoods(1, goodsId);
        }
        [HttpPost]
        [PermissionFilter(permission: Permission.GoodsManager_All)]
        public JsonResult DeleteGoods(int goodsId)
        {
            return OperateGoods(3, goodsId);
        }
        private JsonResult OperateGoods(int op, int goodsId)
        {
            JsonResult res = new JsonResult();
            var success = "";
            var error = "";
            try
            {
                switch (op)
                {
                    case 1: _gservice.OnShelvesGoods(goodsId); break;
                    case 2: _gservice.OffShelvesGoods(goodsId); break;
                    case 3: _gservice.DeleteGoodsById(goodsId); break;
                    default:; break;
                }
                success = "操作成功";
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            res.Data = new { success, error };
            return res;
        }

        [HttpPost]
        [PermissionFilter(permission: Permission.GoodsManager_All)]
        public ActionResult GetGoodsDetail(int goodsId)
        {
            var goods = _gservice.GetGoodsById(goodsId);
            var images = goods.Image.Select(i => new Models.Image()
            {
                Id = i.ImageId,
                AttachmentPath = i.ImageSrc,
            }).ToList();
            NewGoods ng = new NewGoods()
            {
                Name = goods.GoodsName,
                Mark = goods.Details,
                Price = goods.Price,
                Stock = goods.Stock,
                Category = goods.Category,
                Id = goods.GoodsId,
                Images = images,
            };
            return PartialView("/ViewComponents/GoodsDetail", ng);
        }

        public ActionResult GetNewGoods()
        {
            TempData["url"] = "/Goods/AllGoods";
            TempData["name"] = "商品";
            TempData["title"] = "最新商品";
            var goods = _gservice.GetNewGoods(5).Select(g => new AbbreviationsModel()
            {
                CreateTime = g.AddTime.ToString(),
                Name =  g.GoodsName,
                Id = g.GoodsId,
            }).ToList();
            return PartialView("/Admin/NewTips",goods);
        }
    }
}